#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>

using namespace std;
#define NUM 10

struct threadData
{
    string threadname;
};
__thread int g_val = 100;
__thread unsigned int number = 0;
__thread int pid = 0;
void InitThreadData(threadData *td, int number)
{
    td->threadname = "thread-" + to_string(number); // thread-0
}
string toHex(pthread_t tid)
{
    char buffer[128];
    snprintf(buffer, sizeof(buffer), "0x%x", tid);
    return buffer;
}
void *threadRoutine(void *args)
{
    pthread_detach(pthread_self());

    // int test_i = 0;
    threadData *td = static_cast<threadData *>(args);
    // if(td->threadname == "thread-2") p = &test_i;
    string tid = toHex(pthread_self());
    int pid = getpid();

    int i = 0;
    while (i < 10)
    {
        cout << "tid: " << tid << ", pid: " << pid << endl;

        // cout << "pid: " << getpid() << ", tid : "
        //     << toHex(number) << ", threadname: " << td->threadname
        //         << ", g_val: " << g_val << " ,&g_val: " << &g_val <<endl;
        sleep(1);
        i++;
    }

    delete td;
    return nullptr;
}

int main()
{
    vector<pthread_t> tids;
    for (int i = 0; i < NUM; i++)
    {
        pthread_t tid;
        threadData *td = new threadData;
        InitThreadData(td, i);
        int n = pthread_create(&tid, nullptr, threadRoutine, td);
        tids.push_back(tid);
    }

    return 0;
}